<template>
  <div class="user-info-menu">
    <div class="user-info-poppor">
      <template v-if="userInfo && userInfo.userId === 'admin'">
        <el-avatar slot="reference" class="user-icon" :src="circleUrlM" size="medium"></el-avatar>
      </template>
      <template v-else>
        <el-avatar
          slot="reference"
          class="user-icon"
          :src="userInfo.headUrl"
          size="medium"
          v-if="userInfo && userInfo.headUrl"></el-avatar>
        <el-avatar
          slot="reference"
          class="user-icon"
          :src="userInfo && userInfo.gender === '2' ? circleUrlW : circleUrlM"
          size="medium"
          v-else></el-avatar>
      </template>
      <div v-if="userInfo" class="user-info">
        <template v-if="userInfo.userId === 'admin'">
          <span>超级管理员</span>
        </template>
        <template v-else>
          <span>{{ userInfo.userName || userInfo.loginName }}</span>
          <span>{{ userInfo.loginPostInfo.orgName }}</span>
        </template>
      </div>
      <div class="btn-item" @click="logout">
        <img src="@images/home/logout.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import { logout } from '@api/modules/user.js'
import { mapGetters } from 'vuex'
export default {
  name: 'UserInfoIcon',
  components: {},
  props: {},
  data() {
    return {
      circleUrlM: require('@images/user-icon.png'),
      circleUrlW: require('@images/user-icon-G.png'),
    }
  },
  computed: {
    ...mapGetters('role', ['userInfo']),
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    logout() {
      this.$confirm('确定退出登录？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info',
      }).then(() => {
        logout()
          .then(res => {
            if (res.status === 200) {
              this.$message.success('退出成功！')
              this.onLogout()
            }
          })
          .catch(() => {
            this.onLogout()
          })
      })
    },
    onLogout() {
      localStorage.removeItem('token')
      localStorage.clear()
      sessionStorage.clear()
      // 清空 store
      this.$store.commit('clearCommonAll')
      this.$router.replace('/login')
    },
  },
}
</script>
<style lang="scss" scoped>
.user-info-menu {
  cursor: default;
  .user-icon {
    // cursor: pointer;
  }
}
</style>

<style lang="scss">
.user-info-poppor {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #ffffff;
  .user-info {
    padding: 10px;
    display: flex;
    flex-direction: column;
    max-width: 200px;
    margin: 0 10px;
    span:first-child {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .btn-item {
    cursor: pointer;
    height: 45px;
    padding: 10px;
    position: relative;
    font-size: 16px;
    img {
      vertical-align: middle;
      display: inline-block;
      height: 20px;
    }
  }
  .user-icon {
    img {
      width: 100%;
    }
  }
}
</style>
